﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using MachineQA.Model;
using MachineQA.ViewModel;

namespace MachineQA.View
{
    /// <summary>
    /// Interaction logic for LocationsView.xaml
    /// </summary>
    public partial class LocationsView : UserControl
    {
        public LocationsView()
        {
            InitializeComponent();
        }

        private void NewLocationNameBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            TryEnableSaveButton();
        }

        private void TryEnableSaveButton()
        {
            LocationSaveButton.IsEnabled = (NewLocationNameBox.Text != string.Empty &&
                                          (Constants.entities.Locations.
                                          Where((i) => i.Name == NewLocationNameBox.Text).Count() == 0));
        }


        private void LocationsList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            // Check to confirm that there is a selected item and if so make it the selection
            if (e.AddedItems.Count > 0 && e.AddedItems[0] is LocationsViewModel)
                ((LocationsViewModel)this.DataContext).SelectedLocation = (LocationViewModel)e.AddedItems[0];
        }
    }
}
